package com.clinic.dao.imp;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.clinic.dao.interfaces.Lab_analysis_dao;
import com.clinic.domain.Lab_analysis;
import com.clinic.util.Util_imp;

public class Lab_analysis_imp implements Lab_analysis_dao{

	SessionFactory session_factory = null;

	public Lab_analysis_imp() {
		session_factory = Util_imp.get_sessionfactory();
	}

	@Override
	public int create_lab_analysis(Lab_analysis ls) {
		System.out.println("Enter - create_lab_analysis");
		Session session = session_factory.openSession();
		Transaction tx = null;
		int ls_obj_id = 0;
		try {
			tx = session.beginTransaction();
			ls_obj_id = (Integer) session.save(ls);
			tx.commit();
			System.out.println("New Lab_analysis Created with ID: " + ls_obj_id);
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Create Lab_analysis Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - create_lab_analysis");
		return ls_obj_id;
	}

	@Override
	public void update_lab_analysis(Lab_analysis ls) {
		System.out.println("Enter - update_lab_analysis");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(ls);
			tx.commit();
			System.out.println("Lab_analysis Object Updated");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Lab_analysis Rolled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - update_lab_analysis");		
	}

	@Override
	public void delete_lab_analysis(int ls_obj_id) {
		System.out.println("Enter - delete_lab_analysis");
		Session session = session_factory.openSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Lab_analysis ls = (Lab_analysis) session.get(Lab_analysis.class, ls_obj_id);
			session.delete(ls);
			tx.commit();
			System.out.println("Patient with ID: " + ls_obj_id + " Deleted");
		} catch (HibernateException ex) {
			if (tx != null) {
				tx.rollback();
				System.out.println("Now Update Patient Rooled back");
			}
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - delete_lab_analysis");
	}

	@Override
	public Lab_analysis get_lab_analysis(int ls_obj_id) {
		System.out.println("Enter - get_lab_analysis");
		Session session = session_factory.openSession();
		Transaction tx = null;
		Lab_analysis ls = null;
		try {
			tx = session.beginTransaction();
			ls = (Lab_analysis) session.createQuery("from Lab_analysis where ls_obj_id=:id")
					.setParameter("id", ls_obj_id).uniqueResult();
			tx.commit();
			System.out.println("Returned Object Owner Name: " + ls.getObj_owner_name());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_lab_analysis");
		return ls;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Lab_analysis> get_all_lab_analysiss() {
		System.out.println("Enter - get_all_patients");
		Session session = session_factory.openSession();
		List<Lab_analysis> l = null;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			l = session.createQuery("from Lab_analysis").list();
			tx.commit();
			System.out.println("Number of Lab_analysis returned is: " + l.size());
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - get_all_patients");
		return l;
	}

	@Override
	public Lab_analysis persistOrMerge(Lab_analysis ls) {
		return (Lab_analysis) session_factory.getCurrentSession().merge(ls);
	}
	
	@Override
	public int getsize(){
		System.out.println("Enter - getsize");
		Session session = session_factory.openSession();
		int size = 0;
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			size = ((Long) session.createQuery("select count(*) from Lab_analysis").uniqueResult()).intValue();
			tx.commit();
			System.out.println("Count(*) Lab_analysis: " + size);
		} catch (HibernateException ex) {
			ex.printStackTrace();
		} finally {
			session.close();
		}
		System.out.println("Exit - getsize");
		return size;
	}
}